package cn.iocoder.yudao.module.eval.controller.admin.condition;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.module.eval.controller.admin.condition.vo.*;
import cn.iocoder.yudao.module.eval.dal.dataobject.condition.EvalApplicationConditionDO;
import cn.iocoder.yudao.module.eval.service.condition.EvalApplicationConditionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

@Tag(name = "管理后台 - 申报条件配置")
@RestController
@RequestMapping("/eval/application-condition")
@Validated
public class EvalApplicationConditionController {

    @Resource
    private EvalApplicationConditionService applicationConditionService;

    @PostMapping("/create")
    @Operation(summary = "创建申报条件配置")
    @PreAuthorize("@ss.hasPermission('eval:application-condition:create')")
    public CommonResult<Long> createApplicationCondition(@Valid @RequestBody EvalApplicationConditionSaveReqVO createReqVO) {
        return success(applicationConditionService.createApplicationCondition(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新申报条件配置")
    @PreAuthorize("@ss.hasPermission('eval:application-condition:update')")
    public CommonResult<Boolean> updateApplicationCondition(@Valid @RequestBody EvalApplicationConditionSaveReqVO updateReqVO) {
        applicationConditionService.updateApplicationCondition(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除申报条件配置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('eval:application-condition:delete')")
    public CommonResult<Boolean> deleteApplicationCondition(@RequestParam("id") Long id) {
        applicationConditionService.deleteApplicationCondition(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得申报条件配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('eval:application-condition:query')")
    public CommonResult<EvalApplicationConditionRespVO> getApplicationCondition(@RequestParam("id") Long id) {
        EvalApplicationConditionDO applicationCondition = applicationConditionService.getApplicationCondition(id);
        return success(BeanUtils.toBean(applicationCondition, EvalApplicationConditionRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得申报条件配置分页")
    @PreAuthorize("@ss.hasPermission('eval:application-condition:query')")
    public CommonResult<PageResult<EvalApplicationConditionRespVO>> getApplicationConditionPage(@Valid EvalApplicationConditionPageReqVO pageReqVO) {
        PageResult<EvalApplicationConditionDO> pageResult = applicationConditionService.getApplicationConditionPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, EvalApplicationConditionRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出申报条件配置 Excel")
    @PreAuthorize("@ss.hasPermission('eval:application-condition:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportApplicationConditionExcel(@Valid EvalApplicationConditionPageReqVO exportReqVO,
                                                 HttpServletResponse response) throws IOException {
        exportReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<EvalApplicationConditionDO> list = applicationConditionService.getApplicationConditionList(exportReqVO);
        // 导出 Excel
        ExcelUtils.write(response, "申报条件配置.xls", "数据", EvalApplicationConditionRespVO.class,
                BeanUtils.toBean(list, EvalApplicationConditionRespVO.class));
    }

    @GetMapping("/list-by-profession-and-level")
    @Operation(summary = "根据职业代码和技能等级获取申报条件列表")
    @Parameter(name = "professionCode", description = "职业代码", required = true, example = "4-07-05-02")
    @Parameter(name = "skillLevel", description = "技能等级", required = true, example = "五级/初级工")
    @PreAuthorize("@ss.hasPermission('eval:application-condition:query')")
    public CommonResult<List<EvalApplicationConditionRespVO>> getApplicationConditionListByProfessionAndLevel(
            @RequestParam("professionCode") String professionCode, @RequestParam("skillLevel") String skillLevel) {
        List<EvalApplicationConditionDO> list = applicationConditionService.getApplicationConditionListByProfessionAndLevel(professionCode, skillLevel);
        return success(BeanUtils.toBean(list, EvalApplicationConditionRespVO.class));
    }

    @PutMapping("/toggle-status")
    @Operation(summary = "启用/禁用申报条件")
    @Parameter(name = "id", description = "编号", required = true)
    @Parameter(name = "isActive", description = "是否启用", required = true)
    @PreAuthorize("@ss.hasPermission('eval:application-condition:update')")
    public CommonResult<Boolean> toggleApplicationConditionStatus(@RequestParam("id") Long id, @RequestParam("isActive") Boolean isActive) {
        applicationConditionService.toggleApplicationConditionStatus(id, isActive);
        return success(true);
    }

    @PostMapping("/batch-create")
    @Operation(summary = "批量创建申报条件")
    @PreAuthorize("@ss.hasPermission('eval:application-condition:create')")
    public CommonResult<Boolean> batchCreateApplicationConditions(@Valid @RequestBody List<EvalApplicationConditionSaveReqVO> conditions) {
        applicationConditionService.batchCreateApplicationConditions(conditions);
        return success(true);
    }

}
